{% extends "base.html" %}

{% block cur_page_nav %}
{{ macros::current_page_nav(toc=page.toc) }}
{% endblock %}

{% block content %}
    <p class="page-title">{{ page.title }}</p>
    {{ page.content | safe }}
{% endblock content %}

{% block prev_link %}
    {% if page.lighter %}
        {{ macros::nav_prev_item(rel_top=rel_top, title=page.lighter.title, path=page.lighter.path) }}
    {% else %}
        {# No page before, find the link for the section it is in if there is one #}
        {% set parent = get_section(path=page.ancestors | reverse | first) %}
        {{ macros::nav_prev_item(rel_top=rel_top, title=parent.title, path=parent.path) }}
    {% endif %}
{% endblock prev_link %}

{% block next_link %}
    {% if page.heavier %}
        {{ macros::nav_next_item(rel_top=rel_top, title=page.heavier.title, path=page.heavier.path) }}
    {% else %}
        {# No page after, find the link for the following section #}
        {% set index = get_section(path="_index.md") %}
        {% set found_current = false %}
        {% for s in index.subsections %}
            {% set subsection = get_section(path=s) %}
            {% if found_current %}
                {{ macros::nav_next_item(rel_top=rel_top, title=subsection.title, path=subsection.path) }}
                {# no break #}
                {% set_global found_current = false %}
            {% endif %}
            {% for p in subsection.pages %}
                {% if p.permalink == page.permalink %}
                    {% set_global found_current = true %}
                {% endif %}
            {% endfor %}
        {% endfor %}
    {% endif %}
{% endblock next_link %}
